"""
取消订单
"""
import os
import sys
import django
import time

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.extend([BASE_DIR, ])
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "finance_c2c.settings")
django.setup()
from core.tools.edit_photo import EditPicture

if __name__ == '__main__':
    font_file = os.path.join(os.path.dirname(BASE_DIR), "core/font_file/msyh.ttc").replace("\\","/")
    int_time = int(time.time())
    base_image_path = os.path.join(BASE_DIR, "images/2.jpg").replace("\\", "/")
    text = "https://m.kalaibao.com/online/index.html#/pages/buyOilCards1/oil-list?user_id=3"

    qr_code_stream, qr_code_buf = EditPicture.make_qr_code(text)
    # 获取buf
    base_img_file, base_img_buf = EditPicture.open_rb_to_img(base_image_path)
    # 转换尺寸
    qr_code_buf_resize, qr_code_buf_resize_buf = EditPicture.resize_img_by_bytes(qr_code_buf, box=(217, 217))
    # 贴图
    buf, bottom_img = EditPicture.past_img_by_bytes(base_img_buf, qr_code_buf_resize_buf, box=(254, 977))
    # 写文字
    # {font_file: "绝对路径", font_size: 字号, box: (x, y), words: "文字", "color": (0, 0, 0)}
    lines = [{"font_file": font_file, "font_size": 28, "box": (81, 662), "words": "中化石油北苑加油站", "color": (0, 0, 0)},
             {"font_file": font_file, "font_size": 34, "box": (447, 662), "words": "￥6.10", "color": (226, 34, 28)},
             {"font_file": font_file, "font_size": 19, "box": (568, 669), "words": "已降0.70元", "color": (226, 34, 28)},

             {"font_file": font_file, "font_size": 28, "box": (81, 760), "words": "中化石油北苑加油站", "color": (0, 0, 0)},
             {"font_file": font_file, "font_size": 34, "box": (447, 760), "words": "￥6.10", "color": (226, 34, 28)},
             {"font_file": font_file, "font_size": 19, "box": (568, 767), "words": "已降0.70元", "color": (226, 34, 28)},

             {"font_file": font_file, "font_size": 28, "box": (81, 861), "words": "中化石油北苑加油站", "color": (0, 0, 0)},
             {"font_file": font_file, "font_size": 34, "box": (447, 861), "words": "￥6.10", "color": (226, 34, 28)},
             {"font_file": font_file, "font_size": 19, "box": (568, 868), "words": "已降0.70元", "color": (226, 34, 28)},

             {"font_file": font_file, "font_size": 20, "box": (128, 1296), "words": "页面价格为12月31号加油优惠价格(更多优惠扫码了解)", "color": (52, 52, 52)},
             ]
    im, buf = EditPicture.write_words_return(bottom_img, lines)

    popular_url = "https://shop.kalaibao.com/{0}/popular/{1}-{2}.jpg?s={3}".format("shop_test", 3, 10, int_time)
    # 上传oss
    EditPicture.oss_upload_bytes(buf, 'http://oss-cn-beijing.aliyuncs.com', "zaoqi",
                                 "{0}/popular/{1}-{2}.jpg".format(
                                     "shop_test", 3, 10))
    print(popular_url)